上篇介紹了 Pod 與 Service 的功能串接,不知各位是否踩坑了(Wwwwww),上篇提到了在Service後端會有一個ReplicaSet的Pod協助服務不中斷,雖然kubernetes設計上ReplicaSet這個項目基本上已經移轉給Deployment元件去處理,但是Pod Replication Controller 還有在運作,為了驗證一下兩者功能(瞎折騰),我們這篇來介紹一下Deployment 元件 與 驗證一下k3s的ReplicaSet運作機制,並測試一下 增加副本數量、縮減副本數量的功能。
在 kubernetes 中,Deployment元件算是抽象化較為上層的元件,算是 Production 服務的一個模式,提供 ReplicaSet 的服務多重副本機制、Rolling Update 服務滾動更新、Roll Back 服務滾動回復舊版 等機制,透過這幾個機制的輔助,可在更新時進行服務檢測,對於服務更新與恢復舊版進行調整時,對於 Pod 進行版本循序式置換,並將服務接口導向 Service 提供給外部服務使用,提供一個可靠的不停機服務,藉以達成高可靠的服務品質。
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana-deployment
  labels:
    app: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana
k3s kubectl create -f ${deployment-file}
k3s kubectl get deploy -o wide
k3s kubectl get pod -o wide
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: mysql-replicaset
spec:
  replicas: 10
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:latest
        env:
         - name: MYSQL_ROOT_PASSWORD
           value: password  
k3s kubectl create -f ${replicaset-file}

k3s kubectl get pod -o wide
k3s kubectl get rs -o wide
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana-deployment
  labels:
    app: grafana
spec:
  replicas: 10
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana
kubectl create -f ${deployment-file}
k3s kubectl get pod -o wide
k3s kubectl get rs -o wide
k3s kubectl scale --replicas=${replica-count} -f ${file-name}
k3s kubectl scale --replicas=${replica-count} deploy ${deployment-name} 
k3s kubectl get rs -o wide
k3s kubectl scale --replicas=${replica-count} -f ${file-name}
k3s kubectl scale --replicas=${replica-count} deploy ${deployment-name} 
k3s kubectl get rs -o wide
本篇測試以 ReplicaSet 為基礎,透過 Pod ReplicaSet(Pod 副本機制)機制在 ReplicaSet 、Deployment兩個元件上的操作狀態,透過一連串的測試,可看到 ReplicaSet在 k3s是可以實現的,而對於 ReplicaSet 的數量調整,本篇也對於增減 Pod數量的ReplicaSet功能進行驗證,下篇將延續Deployment元件的相關功能,即將前往 Rolling Update與Roll Back的功能進行測試。